//2009-09-01 c.selection().textFrames()みたいな書き方ができるようした。
//2009-08-11 pathItem,textRangeのlengthと衝突する部分を修正。
//2009-07-21 Object,Arrayへの汚染をやめた。

//Curry
Function.prototype.curry = function(){
	var slice = Array.prototype.slice, args = slice.apply(arguments) , that = this;
	return function (){
			return that.apply(this, args.concat (slice.apply(arguments)));	
	};
};


var c;
(function() {
c  = function(s){return new c.prototype.init(s);}
c.selection = c.curry('selection');
c.prototype = {
	'init' : function (s) {
		this.length = 0;
		if(!app.documents)return false;
		if(app.documents.length == 0)return false;
		switch(typeof s){
    		case 'undefined' : return false;
    		case 'function' : return c(s());
    		case 'string' :
    		      if(typeof this.objectShortName[s] == 'string')s= this.objectShortName[s];
    		      try{
					  s = app.activeDocument[s];
					  }catch(e){
						  throw e;
					}
    			  break;
    	}		
    	return  typeof s == 'object' ?  this.add(s) : this.add(app.activeDocument);
	},
	'position' : 0,
	'get' : function(num){if(typeof num == 'number' &&  num  < this.length && num >= 0)return this[num];return false;},
	'now' : function(){return this.get(this.position);},
	'next' : function(){return this.get(this.position++);},
	'prev': function(){return this.get(--this.position);},
	'loop' : function(){
				this.position < this.length || this.position = 0;
				return this.get(this.position++);
	},
	'dofunc' : function(func,arg){
		          if(func.constructor.name == 'Function')func.call(this);
		          return this;
		},
	 'each' : function(fnc,arg) {
	  	    var l = this.length;
	  	    var res = [];
			if(typeof fnc == 'function'){
			for(var i = 0;i<l;i++){
				 ret = fnc.call(this[i],this[i],arg);
				 typeof ret != "undefined" && res.push(ret); 
			 }
			}
			if(res.length > 0){
                return c(res);
			}else{
                return this;
		    }
   	 	},
   	 'add' : function(s){
   	 	        typeof s == 'string' && s = c(s);
				try{
					if(s.constructor.name.match(/^(PathItem|TextRange)$/)){
						this[0] = s;
						this.length = 1;
						return this;
					  }
				}catch(e){}
   	 				switch(typeof s.length){
		 			 case 'number' :
		 			    var s_length = s.length;
		  				for(var i = 0 ; i < s_length; i++)this[this.length+i] = s[i];
		  				this.length = this.length+s.length;
		  				break;
		 			 default : 
		  				this[0] = s;
		  				this.length = 1;
					}
		      return this;
   	 	},
   	 	'filter' : function(){
			if(arguments.length == 0)return this;
			var s = {'TRUE' : [],'FALSE' : []};
			
			for(i=0;i<arguments.length;i++){
				var arg = arguments[i];
				if(typeof arg == "function"){
					while(x = this.next()){
					   arg(x) ? s.TRUE.push(this) : s.FALSE.push(this);
				}
			}else(typeof arg == 'string'){
					if(typeof this.objectShortName[arg] == 'string')arg = this.objectShortName[arg];
					if(typeof this.filters[arg] == 'function')s = this.filters[arg].call(this,s);
			}
		}
		try{
			return c(s.TRUE);
		}catch(e){
			return c();
		}
   	 	},
   	 	'filters' : {},
   	 	'temp' : [],
		'duplicate' : function(x,y){return this.each(function(){return this.duplicate(x,y)})},
		'type' : function(){return this.each(function(){return this.constructor.name})},
		'debug': function(){this.each(function(){$.bp()})},
		'attr' : function(x,y){
			return c(this.each(function(){
				try{
				if(typeof x == 'string' && y ){
						if(typeof y == 'string')return this[x] = y;
						if(typeof y == 'function')this[x] = y.call(this,this);
				}
				if(typeof x == 'object'){
					for(i in x){
							this.hasOwnProperty(i) && (this[i] = x[i]);
					}
				}
				if(typeof x == 'string')return this[x];
				if(!x && !y){
						for(i in this){
							try{
							this.hasOwnProperty(i) && $.writeln(i +" : "+ this[i]);
							}catch(e){
							}
						}
				}
			}catch(e){
				return false;
			}
			}));
			},
		'search' : function(x,y){
			return c(this.each(function(){
				if(typeof this[x] == 'string'){
				 if(this[x] == y)return  this
				}
				if(typeof this[x] == 'object'){
				if(this[x].constructor.name == y)return this;
				}
				}));
		},
		'remove' : function(){this.each(function(){this.remove()})},
		'test' : function(x,fnc,arg){typeof fnc == 'function' && fnc.call(this[x],this[x],arg)}
};
c.extend = function(target,source) {
	for (var property in source){
		//$.writeln(property);
		try{target.prototype[property] = source[property];}catch(e){target[property] = source[property];};
	}
	return target;
	}
c.prototype.init.prototype = c.prototype;

//イラストレーター用
//
var config_for_illustrator = {
    'filters' : {
        'placedItems' : function(s){
                var extract = function(a){a.constructor.name == 'PlacedItem' ? s.TRUE.push(a) : (a.constructor.name == 'GroupItem' ? c(a.pageItems).each(function(){extract(this);}) : s.FALSE.push(a));}
                this.each(extract);
    	 		return s;
            },
        'rasterItems' : function(s){
                var extract = function(a){a.constructor.name == 'RasterItem' ? s.TRUE.push(a) : (a.constructor.name == 'GroupItem' ? c(a.pageItems).each(function(){extract(this);}) : s.FALSE.push(a));}
                this.each(extract);
    	 		return s;  
            },
        'pathItems' : function(s){
      	 		var extract = function(a){a.constructor.name == 'PathItem' ? s.TRUE.push(a)  :  ((a.constructor.name == 'CompoundPathItem' || a.constructor.name == 'GroupItem') ? c(a.pageItems).each(function(){extract(this);}) : s.FALSE.push(a) );}
                this.each(extract);
    	 		return s;
        },
        'pathPoints' : function(s){
      	 		var extract = function(a){a.constructor.name == 'PathItem' ? c(a.pathPoints).each(function(){s.TRUE.push(this)})  :  ((a.constructor.name == 'CompoundPathItem' || a.constructor.name == 'GroupItem') ? c(a.pageItems).each(function(){extract(this);}) : s.FALSE.push(a) );}
                this.each(extract);
                return s;
            },
        'groupItems' : function(s){
                this.each(function(){this.constructor.name == 'GroupItem' ? s.TRUE.push(this) : s.FALSE.push(this);});
                return s;
        },
        'textFrames' : function(s){
                var extract = function(a){a.constructor.name == 'TextFrame' ? s.TRUE.push(a) : (a.constructor.name == 'GroupItem' ? c(a.pageItems).each(function(){extract(this);}) : s.FALSE.push(a));}
                this.each(extract);
				$.writeln(s);
    	 		return s;
        },
        'textRanges' : function(s){
                this.each(function(){
                    this.constructor.name == 'TextRange' && s.TRUE.push(this);
                    this.constructor.name == 'TextFrame' && c(this.textRanges).each(function(tr){s.TRUE.push(tr)});
                });
                return s;
        },
        'selection' : function(s){
                this.each(function(){(this.selected == true ||  this.selected == 'PathPointSelection.ANCHORPOINT') ? s.TRUE.push(this) : s.FALSE.push(this);});
                return s;
        }
},
    'objectShortName' : {
        "I" : 'allPageItems',
    	"T" : 'textFrames', 
    	"G" : 'groupItems', 
    	"P" : 'pathItems', 
    	"PP" : 'pathPoints', 
    	"S" : 'selection', 
    	"PL" : 'placedItems', 
    	"L" : 'layers', 
    	"R" : 'rasterItems',
        }
}

//インデザイン用
//
var config_for_indesign = {
'filters' : {
    'selection' : function(s){},
    'allPageItems' : function(s){
            this.each(function(){
                        c(this.allPageItems).each(function(){
                            s.TRUE.push(this);
                        });
            });
                        return s;
            },
    'groupItems' : function(s){
			this.each(function(){
				this.constructor.name == 'Group' ? s.TRUE.push(this) : s.FALSE.push(this);
			});
			return s;
	},
    'textFrames' : function(s){
            this.each(function(){
                    this.constructor.name == 'TextFrame' ? s.TRUE.push(this) : (this.constructor.name == 'Group'  ? c(this.textFrames).each(function(){s.TRUE.push(this)}) : s.FALSE.push(this));
            }); 
            return s;
    },
	
	'paragraphs' : function(s){
			var textFrames = this.filters.textFrames.call(this,{'TRUE' : [],'FALSE' : []});
			c(textFrames.FALSE).each(function(){
				//$.bp();
				(this.constructor.name == 'Text' || this.constructor.name == 'TextColumn' || this.constructor.name == 'Paragraph' ||  this.constructor.name == 'Cell' )&& textFrames.TRUE.push(this);
				this.constructor.name == 'Table' && c(this.cells).each(function(){textFrames.TRUE.push(this)});
			});
			c(textFrames.TRUE).each(function(){
				c(this.paragraphs).each(function(){s.TRUE.push(this)});
			});
			return s;
	},
	'words' : function(s){
			var textFrames = this.filters.textFrames.call(this,{'TRUE' : [],'FALSE' : []});
			c(textFrames.FALSE).each(function(){
				(this.constructor.name == 'Text' || this.constructor.name == 'TextColumn' || this.constructor.name == 'Paragraph'  || this.constructor.name == 'Cell' ) && textFrames.TRUE.push(this);
				this.constructor.name == 'Table' && c(this.cells).each(function(){textFrames.TRUE.push(this)});
			});
			c(textFrames.TRUE).each(function(){
				c(this.words).each(function(){s.TRUE.push(this)});
			});
			return s;
	},
	'characters' : function(s){
			var textFrames = this.filters.textFrames.call(this,{'TRUE' : [],'FALSE' : []});
			c(textFrames.FALSE).each(function(){
				(this.constructor.name == 'Text' || this.constructor.name == 'TextColumn' || this.constructor.name == 'Paragraph' || this.constructor.name == 'Cell' ) && textFrames.TRUE.push(this);
				this.constructor.name == 'Table' && c(this.cells).each(function(){textFrames.TRUE.push(this)});
			});
			c(textFrames.TRUE).each(function(){
				c(this.characters).each(function(){s.TRUE.push(this)});
			});
			return s;
	},
    'tables' : function(s){
			var textFrames = this.filters.textFrames.call(this,{'TRUE' : [],'FALSE' : []});
			
			c(textFrames.FALSE).each(function(){
				if(this.constructor.name == 'Table' || this.constructor.name == 'Cell' ){
					this.constructor.name == 'Cell' && this.tables.length > 0 && textFrames.TRUE.push(this);
					s.TRUE.push(this);
					}
			});
		
			c(textFrames.TRUE).each(function(){
				c(this.tables).each(
					function(){
						var innertable =this.filters.tables.call(this.cells,{'TRUE' : [],'FALSE' : []});
						c(innertable.TRUE).each(function(){
									this.constructor.name == 'Table' && s.TRUE.push(this);
							});
						s.TRUE.push(this);
						}
				);
			});
			return s;
            },
    'cells' : function(s){
			var tables = this.filters.tables.call(this,{'TRUE' : [],'FALSE' : []});	
			c(tables.TRUE).each(function(){
				c(this.cells).each(function(){s.TRUE.push(this)});
			});
			return s;
		},
    'rows' : function(s){
			var tables = this.filters.tables.call(this,{'TRUE' : [],'FALSE' : []});
			c(tables.TRUE).each(function(){
				c(this.rows).each(function(){s.TRUE.push(this)});
				});
			return s;
		
	},
},
'objectShortName' : {
        "I" : 'pageItems',
    	"T" : 'textFrames', 
    	"G" : 'groupItems', 
    	"P" : 'pathItems', 
    	"PP" : 'pathPoints', 
    	"S" : 'selection', 
    	"PL" : 'placedItems', 
    	"L" : 'layers', 
    	"R" : 'rasterItems',
        }
}

//
if(app.name == 'Adobe Illustrator'){
c.extend(c,config_for_illustrator);
        for(var i in config_for_illustrator.filters){
                c.prototype[i] = c.prototype.filter.curry(i);
        }
};

if(app.name == 'Adobe InDesign'){
	c.extend(c,config_for_indesign);
	        for(var i in config_for_indesign.filters){
                c.prototype[i] = c.prototype.filter.curry(i);
        }
};
})();